package com.niit.user_profile_center.mapper;


import com.niit.user_profile_center.entity.Activity;
import com.niit.user_profile_center.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface UserMapper {
    //根据id查询用户
    @Select("SELECT * FROM tb_user WHERE id = #{id}")

    User selectById(long id);
    //更新用户资料（动态字段更新）
//    @Update("<script>" +
//            "UPDATE tb_user SET " +
//            "<if test='phone != null'>phone = #{phone},</if>" +
//            "<if test='email != null'>email = #{email},</if>" +
//            "<if test='avatar != null'>avatar = #{avatar},</if>" +
//            "<if test='bio != null'>bio = #{bio},</if>" +
//            "<if test='gender != null'>gender = #{gender},</if>" +
//            "update_date = NOW() WHERE id = #{id} AND version = #{version}" +
//            "</script>")
    @Update("<script>" +
            "UPDATE tb_user " +
            "<set> " +
            "  <if test='nickname != null'>nickname = #{nickname},</if> " +
            "  <if test='phone != null'>phone = #{phone},</if> " +
            "  <if test='email != null'>email = #{email},</if> " +
            "  <if test='avatar != null'>avatar = #{avatar},</if> " +
            "  <if test='bio != null'>bio = #{bio},</if> " +
            "  <if test='gender != null'>gender = #{gender}</if> " +
            "</set> " +
            "WHERE id = #{id}" +
            "</script>")
    int updateUserProfile(User user);


//  获取关注信息
    @Select("SELECT u.* FROM tb_user u INNER JOIN tb_follow f ON u.id = f.follower_id WHERE f.following_id = #{userId}")
    List<User> getFollowingUsers(Long userId);
//  获取动态信息
    @Select("SELECT * FROM tb_activity WHERE user_id = #{userId} ORDER BY create_date DESC")
    List<Activity> getUserActivity(Long userId);
    //获取粉丝数
    @Select("SELECT COUNT(*) FROM tb_follow WHERE following_id = #{userId}")
    int getFollowerCount(Long userId);

//  获取关注数
    @Select("SELECT COUNT(*) FROM tb_follow WHERE follower_id = #{id}")
    Integer getFollowingCount(Long id);


    // UserMapper.java
    @Select("SELECT * FROM tb_user WHERE username = #{username}")
    User selectByUsername(String username);


}
